
import GlobalEvent from "../../../cfw/event/GlobalEvent";
import { EventName } from "../../../public/Config";
import CookeryRoomStep from "./CookeryRoomStep";
import CollisionHelper from "../../../cfw/collide/CollisionHelper";
import BagManager from "../../bag/BagManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class CookeryRoomStep1 extends CookeryRoomStep {

    @property(cc.Node)
    handle: cc.Node = null;

    start() {
        this.node.active = true;
        // this.push(x, y, w / 2, h / 2)
        this.handle.on(cc.Node.EventType.TOUCH_START, this.touchStart, this)

    }

    touchCancel() {
    }

    touchStart(e: cc.Touch) {
        let pos = this.handle.convertToNodeSpaceAR(e.getLocation())
        let count = this.handle.childrenCount;
        for (let index = 0; index < count; index++) {
            const element = this.handle.children[index]
            if (CollisionHelper.isPositionInRect(pos, element.getBoundingBox())) {
                BagManager.addCoin(10, element.parent.convertToWorldSpaceAR(element.getPosition()))
                this.handle.removeChild(element)
              
                break;
            }
        }
        count = this.handle.childrenCount;
        if (count <= 0) {
            this.finish()
        }

    }

    finish() {
        cc.log(' finish == ')
        GlobalEvent.instance().emit(EventName.UPDATE_COOKIE_STEP,1)
    }

}
